package com.foreknow.service.impl;

import com.foreknow.dao.*;
import com.foreknow.entity.RuleMap;
import com.foreknow.entity.Scheduling;
import com.foreknow.entity.User;
import com.foreknow.service.SchedulingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
public class SchedulingServiceImpl implements SchedulingService {

    @Autowired
    private SchedulingMapper schedulingMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RegistLevelMapper registLevelMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private RuleMapper ruleMapper;

    @Override
    public List<Scheduling> querySchedulingByTime(String startTime, String endTime) {
        List<Scheduling> list = schedulingMapper.findAllSchedulingByTime(startTime, endTime);
        for (Scheduling value : list) {
            User user = userMapper.findUserById(value.getUserid());
            if (user == null) break;
            value.setUser(user);
            value.setRegistLevel(registLevelMapper.selectByPrimaryKey(user.getRegistleid()));
            value.setDepartment(departmentMapper.findDeptById(user.getDeptid()));
        }
        return list;
    }

    @Override
    public int addScheduling() {
        //日历类
        Calendar calendar = Calendar.getInstance();
        //获取当前时间
        Date date = new Date();
        //将当前时间放到日历类中
        calendar.setTime(date);
        //获取当前月中的总天数
        int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        //要增加的数据
        List<Scheduling> schedulingList = new ArrayList<>();
        //获取到所有的规则
        List<RuleMap> ruleList = ruleMapper.findRuleByQuery(null, null);
        //set不能有重复元素 添加规则中所有科室的id
        HashSet<Integer> deptIds = new HashSet<>();
        //将规则中所有的科室id 添加到数组中
        for (RuleMap rule : ruleList) {
            deptIds.add(rule.getDeptid());
        }
        //为当月中的每一天添加排班的医生
        for (int i = 1; i <= maxDays; i++) {
            //设置为当前月份的日期 从1到最大日期 循环
            calendar.set(Calendar.DAY_OF_MONTH, i);
            //获取这一天的星期（1是周日 2是周1）
            int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            //默认周日是第一天 设置每周的第一天为周一 (周一 1)
            if (weekDay == 0) {
                weekDay = 7;
            }
            //在week字段中的索引 从0开始
            int morningIndex = weekDay * 2 - 2;//星期一上午 0  星期二上午 2
            int afternoonIndex = weekDay * 2 - 1;//星期一 下午 1  星期二下午3
            //根据科室id循环添加 保证每天每个科室都有医生排班
            for (Integer deptId : deptIds) {
                //为一天中上午有空的医生排班
                for (RuleMap rule : ruleList) {
                    if (rule.getDeptid() != deptId) continue; //如果规则的科室id不等于要排班的科室id 进入下一次循环
                    if (rule.getWeek().charAt(morningIndex) == '1') {//如果上午有时间
                        Scheduling scheduling = new Scheduling();
                        scheduling.setScheddate(calendar.getTime());
                        scheduling.setDeptid(rule.getDeptid());
                        scheduling.setUserid(rule.getUserid());
                        scheduling.setNoon("上午");
                        scheduling.setRuleid(rule.getId());
                        schedulingList.add(scheduling);
                        break;
                    }
                }
                //为一天中下午有空的医生排班
                for (RuleMap rule : ruleList) {
                    if (rule.getDeptid() != deptId) continue; //如果规则的科室id不等于要排班的科室id 进入下一次循环
                    if (rule.getWeek().charAt(afternoonIndex) == '1') {//如果下午有时间
                        Scheduling scheduling = new Scheduling();
                        scheduling.setScheddate(calendar.getTime());
                        scheduling.setDeptid(rule.getDeptid());
                        scheduling.setUserid(rule.getUserid());
                        scheduling.setNoon("下午");
                        scheduling.setRuleid(rule.getId());
                        schedulingList.add(scheduling);
                        break;
                    }
                }
            }
        }
        //清除当月的排班数据
        calendar.set(Calendar.DAY_OF_MONTH, 1);//设置日期为当月的第一天
        Date startTime = calendar.getTime();
        calendar.set(Calendar.DAY_OF_MONTH, maxDays);//设置日期为当月最后一天
        Date endTime = calendar.getTime();
        schedulingMapper.deleteBytime(startTime, endTime);//删除这个月的排班规则
        //重新生成排班数据
        return schedulingMapper.insertScheduling(schedulingList);
    }
}